﻿// -----------------------------------------------------------------------
// <copyright file="Email.cs" company="The Usual Dosage">
// © 2013 by Matthew M. Martin
// </copyright>
// -----------------------------------------------------------------------


namespace Cadence.NET.Notifications
{
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Net;
    using System.Net.Mail;
    using Cadence.NET.Interfaces;

    /// <summary>
    /// Sends an email message over SMTP using the specified properties
    /// </summary>
    public class Email : INotificationService
    {
        #region Public Properties

        string smtpServer = string.Empty;
        /// <summary>
        /// Gets or sets the SMTP server.
        /// </summary>
        /// <value>
        /// The SMTP server.
        /// </value>
        public string SMTPServer 
        {
            get
            {
                smtpServer = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SMTPServer"])) ? ConfigurationManager.AppSettings["SMTPServer"] : null;
                return smtpServer;
            }
            set
            {
                smtpServer = value;
            }
        }

        int smtpPort = 587;
        /// <summary>
        /// Gets or sets the SMTP port.
        /// </summary>
        /// <value>
        /// The SMTP port.
        /// </value>
        public int SMTPPort
        {
            get
            {
                smtpPort = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SMTPPort"])) ? int.Parse(ConfigurationManager.AppSettings["SMTPPort"]) : 587;
                return smtpPort;
            }
            set
            {
                smtpPort = value;
            }
        }

        string smtpNotificationSubjectLine = string.Empty;
        /// <summary>
        /// Gets or sets the SMTP notification subject line.
        /// </summary>
        /// <value>
        /// The SMTP notification subject line.
        /// </value>
        public string SMTPNotificationSubjectLine
        {
            get
            {
                smtpNotificationSubjectLine = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SMTPNotificationSubjectLine"])) ? ConfigurationManager.AppSettings["SMTPNotificationSubjectLine"] : null;
                return smtpNotificationSubjectLine;
            }
            set
            {
                smtpNotificationSubjectLine = value;
            }
        }

        List<string> smtpToAddresses = null;
        /// <summary>
        /// Gets or sets the SMTP to addresses.
        /// </summary>
        /// <value>
        /// The SMTP to addresses.
        /// </value>
        public List<string> SMTPToAddresses
        {
            get
            {
                smtpToAddresses = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SMTPToAddresses"])) ? ConfigurationManager.AppSettings["SMTPToAddresses"].Split(',').ToList() : null;
                return smtpToAddresses;
            }
            set
            {
                smtpToAddresses = value;
            }
        }

        string smtpFromAddress = string.Empty;
        /// <summary>
        /// Gets or sets the SMTP from address.
        /// </summary>
        /// <value>
        /// The SMTP from address.
        /// </value>
        public string SMTPFromAddress
        {
            get
            {
                smtpFromAddress = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SMTPFromAddress"])) ? ConfigurationManager.AppSettings["SMTPFromAddress"] : null;
                return smtpFromAddress;
            }
            set
            {
                smtpFromAddress = value;
            }
        }

        string smtpAuthUsername = string.Empty;
        /// <summary>
        /// Gets or sets the SMTP auth username.
        /// </summary>
        /// <value>
        /// The SMTP auth username.
        /// </value>
        public string SMTPAuthUsername
        {
            get
            {
                smtpAuthUsername = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SMTPAuthUsername"])) ? ConfigurationManager.AppSettings["SMTPAuthUsername"] : null;
                return smtpAuthUsername;
            }
            set
            {
                smtpAuthUsername = value;
            }
        }

        string smtpAuthPassword = string.Empty;
        /// <summary>
        /// Gets or sets the SMTP auth password.
        /// </summary>
        /// <value>
        /// The SMTP auth password.
        /// </value>
        public string SMTPAuthPassword
        {
            get
            {
                smtpAuthPassword = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SMTPAuthPassword"])) ? ConfigurationManager.AppSettings["SMTPAuthPassword"] : null;
                return smtpAuthPassword;
            }
            set
            {
                smtpAuthPassword = value;
            }
        }

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        /// Sends the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public bool Send(string message)
        {
            try
            {

                MailMessage mail = new MailMessage();
                mail.From = new MailAddress(SMTPFromAddress);
                SMTPToAddresses.ForEach(a =>
                {
                    mail.To.Add(a);
                });

                mail.Subject = SMTPNotificationSubjectLine;
                mail.Body = message;

                using (SmtpClient client = new SmtpClient())
                {
                    client.Port = SMTPPort;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;

                    if (!string.IsNullOrEmpty(SMTPAuthPassword))
                    {
                        client.Credentials = new NetworkCredential(SMTPAuthUsername, SMTPAuthPassword);
                    }

                    client.EnableSsl = true;
                    client.Host = SMTPServer;
                    client.UseDefaultCredentials = false;
                    client.Send(mail);
                }

                return true;
            }
            catch
            {
                throw;
            }
        }

        #endregion Public Methods
    }
}
